#!/bin/sh
<<!
  gitlab安装脚本
!
#=============参数配置=========
#下载地址
downloadUri=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/
#安装包名称
packageName=gitlab-ce
#下载版本
packageVersion=10.7.3-ce.0.el7.x86_64
#安装包后缀
packageSuffix=.rpm
#注册服务单元名称
packageServeiceName=gitlab
sep=-
fullPackageName=$packageName$sep$packageVersion$packageSuffix
fullDownloadUrl=$downloadUri$fullPackageName
currentIp=$(ip addr show |grep "inet " |grep -v '127.0.0.1' |awk '{print $2}'|awk 'BEGIN{FS="/"}{print $1}' |head -1)
#=================================================
#========         日志脚本引用代码         ==========
#=================================================
#Shell脚本所在目录
shellPath=$(cd `dirname $0`; pwd);
#日志脚本文件是否存在
logshExist=$(ls ./log.sh &>/dev/null; echo $?);
#是否下载过脚本文件,如果下载过,无论成功失败都不会再次下载.
downLoaded=0;
#下载日志脚本
if [ $logshExist -gt 0 -a  $downLoaded -eq 0 ]
  then
    wget -Ncq http://gitee.com/topanda/note/raw/master/linux/log.sh &>/dev/null;
    #重置下载标记
    downLoaded=1;
fi
#修改日志脚本执行权限
chmod u+x log.sh
logshExist=$(`ls ./log.sh &>/dev/null`; echo $?);
#如果不存在日志文件,默认什么都不做.
function log(){
  if [  $logshExist -eq 0 ]
    then
    logPath=$shellPath/log.sh
    $logPath $*;
  fi
}
#================================================
#================================================
#=========    错误日志监听方法    ===============
#父进程的pid.
basePid=$(echo $$);
#默认杀父进程
killP=0;
function listion(){
while read line;do
  if [ ${#line} -eq 0 ]; then
    exit 0;
  fi
  err=$(echo $line |grep  -e "^err\(or\)\?\(:\)\?[[:space:]]" -e "^失败\(:\)\?[[:space:]]"  -e "^错误\(:\)\?[[:space:]]");
  errLen=$(echo ${#err});
  if [  $errLen -gt 0 ]
  then
    log ERR $line;
    log ERR "发生错误,程序中断,请检查日志..."
    if [ $killP -gt 0  ]
    then
      kill -15 $basePid;
    fi
    exit 1;
  fi
  warn=$(echo $line |grep  -e "^warn\(ing\)\?\(:\)\?[[:space:]]" -e "警告\(:\)\?[[:space:]]")
  warnLength=$(echo ${#warn})
  if [  $warnLength -gt 0 ]
  then
    log WARN $line;
  else
    log INFO $line;
  fi
  unset err;
  unset errLen;
  unset content;
  unset length;
done;
}
#===========================================================
# step1: 安装gitlib依赖以及下载gitlab安装包
# step2: 通过yum安装gitlab,并初始化配置.
# step3: 开放80端口.
#===========================================================
#=======================业务代码=============================
#===========================================================
#step1: 初始化环境
log INFO "初始化安装环境,下载安装:curl openssh-server openssh-clients postfix cronie lokkit"
yum install -y curl openssh-server openssh-clients postfix cronie lokkit |listion
systemctl start postfix 1>/dev/null  | listion
systemctl enable postfix 1>/dev/null  | listion
lokkit -s http -s ssh 1>/dev/null  | listion
#step2:下载gitlab安装包
log INFO "\033[31;05m下载gitlab文件中...\033[0m"
packageExist=$(`ls ./$fullPackageName 2>&1>/dev/null`; echo $?);
#------------------------------------------------------------------------------
if [ $packageExist -gt 0 ]
  then
    wget -Nc $fullDownloadUrl  | listion
else
    log INFO "文件已存在,跳过下载."
fi
#------------------------------------------------------------------------------
log INFO "安装gitlab软件中..."
if [ `rpm -qa |grep $packageName || yum list installed |grep $packageName` ]
  then
    log WARN  "Soft $packageName installed";
  else
    yum install -y $fullPackageName  | listion
fi
#------------------------------------------------------------------------------
#备份gitlab数据
log INFO "尝试备份数据..."
gitlab-rake gitlab:backup:create | listion
#重新配置gitlab服务.
log INFO "\033[31;05m 重新配置gitlab服务,比较耗时,请耐心等待. \033[0m"
log WARN "展示重新配置的过程...."
gitlab-ctl reconfigure
#step3:防火墙设置
portEnabled=$(`firewall-cmd --query-port=80/tcp &>/dev/null`;echo $?);
if [[ $portEnabled -gt 0 ]];
  then
    log INFO "修改防火墙设置..."
    firewall-cmd --zone=public --add-port=80/tcp --permanent  | listion
    firewall-cmd --reload  | listion
fi
log INFO "脚本安装完毕..."
log INFO "请使用http://$currentIp:80来访问gitlab服务..."
log INFO "第一次访问需要将会进入设置密码页面"
log INFO "默认用户名称为root."
log INFO "脚本默认备份了已有的gitlab数据,请注意查看日志."
